package com.zz.parkingsystem.controller.user;

import com.zz.parkingsystem.entity.Order;
import com.zz.parkingsystem.entity.OrderItem;
import com.zz.parkingsystem.service.CarService;
import com.zz.parkingsystem.service.OrderItemService;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("orderItem")
@Api(value = "",tags = "订单项接口")
public class UserOrderItemController {
    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private CarService carService;
    //使用中
    //用户输入泊位号,和车牌，判断该泊位号的情况（是否车牌为空，是否泊位被占用，是否无效泊位号）
    //判断该车牌是否属于这个用户，判断该车牌是否存在
    @GetMapping("/selParkingByPkNo/{pkNo}/{carLicensePlate}/{accId}")
    public Result selParkingByPkNo(@PathVariable Integer pkNo,@PathVariable String carLicensePlate,@PathVariable Integer accId){
        String message = orderItemService.selParkingByPkNo(pkNo,carLicensePlate,accId);
        if(message.equals("无异常认领订单")){
            return ResultGenerator.genSuccessResult("无异常认领订单");
        }
        return ResultGenerator.genFailResult(message);
    }
    //使用中
    //用户点击
    //用户id,车辆id（由车牌号可查）,泊位id，路段id，停车图片等由前端传入
    @PostMapping("/addOrderItem")
    public Result createOrderItem(@RequestBody OrderItem orderItem){
        String message = orderItemService.insertSelectiveUser(orderItem);
        if(message.contains("订单项创建成功，用户可以认领订单了")){
            return ResultGenerator.genSuccessResult(message);
        }
        return ResultGenerator.genFailResult(message);
    }
    //用户停车后，将生成好的订单项返回给前端（包含订单项id,订单流水号，用户id，车辆id,泊位id,路段id,驶入时间）
    @GetMapping("selOrderItem/{oiNo}")
    public Result selOrderItem(@PathVariable String oiNo){
        return ResultGenerator.genSuccessResult(orderItemService.selOrderItemByOiNo(oiNo));
    }
    //使用中
    //用户车辆离开后,产生离开时间，计算金额,修改订单状态为未支付，修改泊位状态为无车
    @PostMapping("/carLeave/{OiNo}")
    public Result carLeave(@PathVariable String OiNo) {
        OrderItem orderItem = orderItemService.selOrderItemByOiNo(OiNo);
        orderItemService.updateByPrimaryKeySelectiveUser(orderItem);
        return ResultGenerator.genSuccessResult("车辆离开，用户可以付款了");
    }
    //使用中
    //通过用户id查该用户所有的订单项
    @GetMapping("/selAllOrderItemByUserId/{userId}")
    public Result selAllOrderItemByUserId(@PathVariable Integer userId){
        List<OrderItem> list = orderItemService.selAllOrderItemByUserId(userId);
        if(list.size()==0){
            return ResultGenerator.genFailResult("该用户还没有产生任何订单项");
        }
        return ResultGenerator.genSuccessResult(list);
    }
}
